<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="租户姓名" prop="tenantName">
        <el-input
          v-model="queryParams.tenantName"
          placeholder="请输入租户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户手机号" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入租户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="合同金额" prop="contractAmount">
        <el-input
          v-model="queryParams.contractAmount"
          placeholder="请输入合同金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zlgl:roomRent:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zlgl:roomRent:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zlgl:roomRent:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zlgl:roomRent:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="roomRentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="房间号" align="center" prop="room" />
      <el-table-column label="所属小区" align="center" prop="community" />
      <el-table-column label="所属楼座" align="center" prop="buildingNumber" />
      <el-table-column label="单元号" align="center" prop="unitNumber" />
      <el-table-column label="租户姓名" align="center" prop="tenantName" />
      <!-- <el-table-column label="租赁状态" align="center" prop="rentStatus" /> -->
      <el-table-column label="租户手机号" align="center" prop="phoneNumber" />
      <!-- <el-table-column label="合同金额" align="center" prop="contractAmount" /> -->
      <el-table-column label="备注信息" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['zlgl:roomRent:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zlgl:roomRent:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改房屋租赁对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="小区" prop="communityId">
          <el-select
            v-model="queryWbu.communityId"
            placeholder="请选择小区"
            @keyup.enter.native="handleQuery"
          >
            <el-option
              v-for="item in communityList"
              :key="item.communityId"
              :label="item.communityName"
              :value="item.communityId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼座" prop="balconyId">
          <el-select
            v-model="queryWbu.balconyId"
            placeholder="请选择楼座"
            @keyup.enter.native="handleQuery"
          >
            <el-option
              v-for="item in balconyList"
              :key="item.balconyId"
              :label="item.balconyName"
              :value="item.balconyId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单元" prop="unitId">
          <el-select
            v-model="queryWbu.unitId"
            placeholder="请选择单元"
            @keyup.enter.native="handleQuery"
          >
            <el-option
              v-for="item in unitList"
              :key="item.unitId"
              :label="item.unitName"
              :value="item.unitId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房号" prop="homeId">
          <el-select
            v-model="queryWbu.homeId"
            placeholder="请选择房号"
            @keyup.enter.native="handleQuery"
          >
            <el-option
              v-for="item in homeList"
              :key="item.homeId"
              :label="item.homeNumber"
              :value="item.homeId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="租户姓名" prop="tenantName">
          <el-input v-model="form.tenantName" placeholder="请输入租户姓名" />
        </el-form-item>
        <el-form-item label="租户手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入租户手机号" />
        </el-form-item>
        <!-- <el-form-item label="合同金额" prop="contractAmount">
          <el-input
            v-model="form.contractAmount"
            placeholder="请输入合同金额"
          />
        </el-form-item> -->
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRoomRent, getRoomRent, delRoomRent, addRoomRent, updateRoomRent } from "@/api/zlgl/roomRent";
import { cascade } from "@/api/basic/unitInfo.js"
import {
		getCommunity,
		getBalconyUnitHome
	} from "@/api/charge/allocation";

export default {
  name: "RoomRent",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房屋租赁表格数据
      roomRentList: [],
      // 弹出层标题
      title: "",
      // 小区列表数据
				communityList: [],
				// 楼座列表数据
				balconyList: [],
				// 单元列表数据
				unitList: [],
				// 房间列表数据
				homeList: [],
				queryWbu: {
					communityId: null,
					balconyId: null,
					unitId: null,
					homeId: null
				},
  
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        room: null,
        community: null,
        buildingNumber: null,
        unitNumber: null,
        tenantName: null,
        rentStatus: null,
        phoneNumber: null,
        contractAmount: null,
      },
      // 表单参数
      form: {
        id: null,
        room: null,
        community: null,
        buildingNumber: null,
        unitNumber: null,
        tenantName: null,
        rentStatus: null,
        phoneNumber: null,
        contractAmount: null,
        remark: null
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    // this.getProps();
    this.getCommunityList();
  },
  methods: {
    /* 获取小区列表 */
			getCommunityList() {
				getCommunity().then(res => {
					this.communityList = res.data
				})
			},
    /** 查询房屋租赁列表 */
    getList() {
      this.loading = true;
      listRoomRent(this.queryParams).then(response => {
        this.roomRentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.queryWbu= {
					communityId: null,
					balconyId: null,
					unitId: null,
					homeId: null
				}
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        room: null,
        community: null,
        buildingNumber: null,
        unitNumber: null,
        tenantName: null,
        rentStatus: null,
        phoneNumber: null,
        contractAmount: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加房屋租赁";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRoomRent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改房屋租赁";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRoomRent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.queryWbu= {
					communityId: null,
					balconyId: null,
					unitId: null,
					homeId: null
				}
            });
          } else {
            addRoomRent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.queryWbu= {
					communityId: null,
					balconyId: null,
					unitId: null,
					homeId: null
				}
            });
          }
        }
      });
    },
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除房屋租赁编号为"' + ids + '"的数据项？').then(function() {
        return delRoomRent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zlgl/roomRent/export', {
        ...this.queryParams
      }, `roomRent_${new Date().getTime()}.xlsx`)
    }
  },
  watch: {
			'queryWbu.communityId': {
				handler() {
					this.balconyList = []
					this.unitList = []
					this.homeList = []
					this.queryWbu.balconyId = null
					this.queryWbu.unitId = null
					this.queryWbu.homeId = null
					
					getBalconyUnitHome(this.queryWbu).then(res => {
						this.balconyList = res.data[0]
						// if (res.data.length > 3) {
						// 	this.queryParams.balconyunithomeIds = res.data[3]
						// 	this.form.balconyunithomeIds = res.data[3]
						// }
						// this.queryParams.communicateId = this.queryWbu.communityId
						// this.form.communicateId = this.queryWbu.communityId
					})
				},
				deep: true,
				immediate: true
			},
			'queryWbu.balconyId': {
				handler() {
					this.unitList = []
					this.homeList = []
					this.queryWbu.unitId = null
					this.queryWbu.homeId = null
					
					getBalconyUnitHome(this.queryWbu).then(res => {
						this.unitList = res.data[1]
					})
				},
				deep: true,
				immediate: true
			},
			'queryWbu.unitId': {
				handler() {
					this.homeList = []
					this.queryWbu.homeId = null
					
					getBalconyUnitHome(this.queryWbu).then(res => {
						this.homeList = res.data[2]
					})
				},
				deep: true,
				immediate: true
			},
			'queryWbu.homeId': {
				// handler() {
					
					// getBalconyUnitHome(this.queryWbu).then(res => {
					
					// }).then(()=>{
          //   // console.log(this.communityList)
          //   // console.log(this.balconyList)
          //   // console.log(this.unitList)
          //   // console.log(this.homeList)
          //   if(this.queryWbu.homeId!=null){
          //     this.form.community=this.communityList[this.queryWbu.communityId].communityName
          //     this.form.buildingNumber=this.balconyList[this.queryWbu.balconyId].balconyName
          //     this.form.unitNumber=this.unitList[this.queryWbu.unitId].unitName
          //     this.form.room=this.homeList[this.queryWbu.homeId].homeNumber
          //     console.log(this.form.community)
          //     console.log(this.form.buildingNumber)
          //     console.log(this.form.unitNumber)
          //     console.log(this.form.room)
          //   }
          // }
          // )
          handler(newId) {
					this.form.homeLocation = null
					if (newId) {
						getBalconyUnitHome(this.queryWbu).then(res => {
							getCommunity(this.queryWbu).then(response => {
								// this.form.homeLocation = response.data[0].communityName + res.data[0][0]
								// 	.balconyName + res.data[1][0].unitName + res.data[2][0].homeNumber +
								// 	"房间"
								// console.log(this.form.homeLocation)
              this.form.community=response.data[0].communityName
               this.form.buildingNumber=res.data[0][0].balconyName
               this.form.unitNumber=res.data[1][0].unitName
               this.form.room=res.data[2][0].homeNumber
							})
						})
					}
				}
				},
				deep: true,
				immediate: true
			}
		
};
</script>
